﻿using System;
using System.Collections;
using System.Collections.Generic;
using KFGame;
using UnityEngine;
namespace KFGame
{
    //读取一天的数据
    public class DaysLog
    {
        public string logtype = "day";

        KFDataFile dataFile = new KFDataFile();

        public List<String> listlabes = new List<string>();

        public List<long> listDatas = new List<long>();

        string title;

        int currday;

        public DaysLog(string n)
        {
            title = n;
            DateTime t = DateTime.Now;
            currday = t.Day;
        }

        //加载偏移天数时间，如0 就是当天，-1 前天 1 后一天
        public void load(double Offsetday = 0)
        {
            DateTime t = DateTime.Now;
            t = t.AddDays(Offsetday);

            dataFile.title = title;
            dataFile.type = logtype;
            dataFile.consent = t.ToString("yyyy_MM_dd");

            string[] datas = dataFile.read();
            if (datas == null) return;
            if (datas.Length < 2) return;

            string[] labs = datas[0].Split(' ');
            listlabes = new List<string>(labs);

            string[] das = datas[1].Split(' ');
            List<string> dass = new List<string>(das);
            listDatas = dass.ConvertAll(s => long.Parse(s));
        }

        //添加数据
        public void addDatas(long d)
        {


            if (listlabes.Count == 0)
                listlabes.Add(currday.ToString());
            else
                listlabes[0] = currday.ToString();

            if (listDatas.Count == 0)
                listDatas.Add(d);
            else
                listDatas[0] += d;

        }

        //保存数据
        public void save()
        {
            string[] data = new string[2];
            data[0] = string.Join("", listlabes.ToArray());
            data[1] = string.Join("", listDatas.ToArray());
            dataFile.save(data);
        }
    }
}